package com.xca.znb.view;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.entity.Account;
import com.xca.znb.entity.User;
import com.xca.znb.model.impl.BillingImpl;
import com.xca.znb.presenter.BillingPresenter;

import java.util.ArrayList;
import java.util.List;

public class BillingActivity extends AppCompatActivity {
    private BillingPresenter presenter ;

    private ImageButton back ;
    private ListView mList ;
    private ArrayAdapter<Account> adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.billing);
        back = findViewById(R.id.billing_back_btn);
        mList = findViewById(R.id.billing_list) ;

        BillingImpl billing = new BillingImpl(this) ;
        presenter = new BillingPresenter(billing);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");

        List<Account> accountList = presenter.getAccountByPhone(phone);
        User user = presenter.getUser(phone);
        String showName = user.getName();


        adapter = new ArrayAdapter<Account>(this , R.layout.billing_item , R.id.billing_show_name_or_id , accountList){
            @Override
            public View getView(int position , View converView , ViewGroup parent){
                View view = super.getView(position , converView , parent);
                TextView name = view.findViewById(R.id.billing_show_name_or_id);
                TextView time = view.findViewById(R.id.billing_show_time);
                TextView type = view.findViewById(R.id.billing_show_type);
                TextView num = view.findViewById(R.id.billing_show_num);
                Account account = getItem(position);
                if (showName != null){
                    name.setText(showName);
                }else {
                    name.setText(phone);
                }
                time.setText(account.getTime_sec());
                type.setText(account.getType());
                double getNums = account.getNum();
                String nums = String.valueOf(getNums);
                num.setText(nums);

                return view ;
            }
        };

        mList.setAdapter(adapter);

    }
}
